package com.jame.basics.thread.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author : Jame
 * @date : 2021-01-15 15:38
 **/
public class FutureTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//                    try {
//                        TimeUnit.SECONDS.sleep(2);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//        );
//        future.get();//这个会阻塞main线程,当future执行完后才会执行main线程下面的代码
//        System.out.println("---");
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                int i=1/0;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "print";
        });

        future.whenComplete((t,u)->{
            System.out.println(t+"=="+u);
        }).exceptionally((e)->{
            System.out.println(e);
            return "失败";
        });
        String s = future.get();

        System.out.println(s);

    }
}
